export default function ({url="", type="get", data={}, timeout=10000}={}){
    let str = "";
    for(let i in data){
        str += `${i}=${data[i]}&`;
    }
    url = type==="get" ? url+"?"+str.slice(0,-1) : url;

    const xhr = new XMLHttpRequest();
    xhr.open(type, url);
    xhr.timeout = timeout;
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(type==="post" ? str.slice(0,-1) : undefined);
    return new Promise((resolve, reject)=>{
        xhr.onload = function(){
            if(xhr.status === 200){
                resolve(xhr.responseText);
            }else{
                reject(xhr.status);
            }
        }
        xhr.ontimeout = function(){
            reject("timeout");
            xhr.abort();
        }
    })
}